package com.xxx.video.commen.utils;

import android.media.MediaPlayer;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;

public class PlayVoiceUtils {

    private static PlayVoiceUtils instance;
    /**
     *
     */
    private MediaPlayer mMediaPlayer;
    /**
     *
     */
    private boolean isPlaying;
    /**
     *
     */
    private OnCompletionListener onCompletionListener;
    /**
     *
     */
    private String voiceUrl = "";

    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (mMediaPlayer != null) {
                /**
                 *
                 */
                int position = mMediaPlayer.getCurrentPosition();
                if (onCompletionListener != null) {
                    onCompletionListener.onProgress(position);
                    mHandler.sendMessageDelayed(new Message(), 200);
                }
            }

        }
    };


    private PlayVoiceUtils() {

    }

    public static PlayVoiceUtils getInstance() {
        if (instance == null) {
            synchronized (PlayVoiceUtils.class) {
                if (instance == null) {
                    instance = new PlayVoiceUtils();
                }
            }
        }
        return instance;
    }


    /**
     * @param voice
     * @param msec
     */
    public void startPlayVoice(String voice, int msec) {
        if (TextUtils.isEmpty(voice)) {
            return;
        }
        this.voiceUrl = voice;
        if (mMediaPlayer != null) {
            mMediaPlayer.stop();
            mMediaPlayer.release();
        }
        mMediaPlayer = null;
        mMediaPlayer = new MediaPlayer();
        try {
            mMediaPlayer.setDataSource(voice);
            mMediaPlayer.prepareAsync();
            mMediaPlayer.setOnPreparedListener(mp -> {
                if (msec != 0) {
                    mMediaPlayer.seekTo(msec);
                }
                mMediaPlayer.start();
                if (onCompletionListener != null) {
//                    onCompletionListener.onStart();
                }
                isPlaying = true;
                mHandler.sendMessageDelayed(new Message(), 200);
            });
            mMediaPlayer.setOnCompletionListener(mp -> {
                if (onCompletionListener != null) {
//                    onCompletionListener.onCompletion();
                }
                stopVoice(false);
            });
        } catch (Exception e) {
            e.printStackTrace();
            stopVoice(true);
        }
    }

    public void setOnCompletionListener(OnCompletionListener onCompletionListener) {
        this.onCompletionListener = onCompletionListener;
    }

    /**
     *
     */
    public void stopVoice(boolean isStop) {
        if (mMediaPlayer != null) {
            mMediaPlayer.stop();
            mMediaPlayer.release();
            mMediaPlayer = null;
        }
        isPlaying = false;
        if (isStop) {
            if (onCompletionListener != null) {
                onCompletionListener.onPlayStop();
            }
        }

    }

    /**
     *
     */
    public void reStart() {
        if (mMediaPlayer != null) {
            mMediaPlayer.seekTo(0);
            mMediaPlayer.start();
        }
    }

    public interface OnCompletionListener {

        /**
         * @param progress
         */
        void onProgress(int progress);

        void onPlayStop();
    }

    /**
     * @return
     */
    public boolean isPlaying() {
        return isPlaying;
    }

    /**
     * @return
     */
    public String getVoiceUrl() {
        return voiceUrl;
    }
}
